package com.luocj.muke.demo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.luocj.muke.demo.R;
import com.luocj.muke.demo.interIml.OnAddClickedListener;
import com.luocj.muke.demo.interIml.OnMinusClickedListener;
import com.luocj.muke.demo.model.ShopCartModel;

import java.util.ArrayList;
import java.util.List;

public class ShopcartAdapter extends RecyclerView.Adapter<ShopcartAdapter.Holder> {

    private static final String TAG = ShopcartAdapter.class.getSimpleName();
    private Context mContext;
    private List<ShopCartModel> mDatas = new ArrayList<>();
    private boolean isItemSelected = false;
    private LayoutInflater inflater = null;
    private OnAddClickedListener onAddClickedListener;
    private OnMinusClickedListener onMinusClickedListener;

    public ShopcartAdapter(Context context, ArrayList<ShopCartModel> shopCartModels) {
        this.mContext = context;
        this.mDatas = shopCartModels;
        inflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = inflater.inflate(R.layout.item_shopcart, parent, false);
        Holder holder = new Holder(inflate);
        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull final Holder holder, final int position) {
        ShopCartModel shopCartModel = mDatas.get(position);
        holder.price.setText(shopCartModel.getPrice() + "");
        holder.num.setText(shopCartModel.getNum()+"");
        holder.name.setText(shopCartModel.getName());
        if (shopCartModel.isChecked()) {
            holder.isSelected.setImageResource(R.mipmap.ssdk_oks_classic_check_checked);
        } else {
            holder.isSelected.setImageResource(R.mipmap.ssdk_oks_classic_check_default);
        }

        holder.btnDelate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClearBtnClick.onClearClickListener(position);
            }
        });



        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onAddClickedListener.OnAddClicked(position);
            }
        });

        holder.minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
onMinusClickedListener.OnMinusClicked(position);
            }
        });

        holder.isSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isItemSelected = !mDatas.get(position).isChecked();
                onItemSelected.OnItemSelected(position, isItemSelected);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class Holder extends RecyclerView.ViewHolder {
        private TextView price;
        private TextView name;
        private TextView num;
        private ImageView isSelected;
        private ImageView add;
        private ImageView minus;
        private Button btnDelate;

        public Holder(View itemView) {
            super(itemView);
            price = itemView.findViewById(R.id.tv_shop_price);
            name = itemView.findViewById(R.id.tv_shop_name);
            isSelected = itemView.findViewById(R.id.iv_check);
            btnDelate = itemView.findViewById(R.id.btn_clear);
            add = itemView.findViewById(R.id.iv_add);
            minus = itemView.findViewById(R.id.iv_minus);
            num = itemView.findViewById(R.id.tv_num);
        }
    }

    public interface onClearBtnClick {
        void onClearClickListener(int position);
    }

    private onClearBtnClick onClearBtnClick;

    public void setOnClearBtnClick(ShopcartAdapter.onClearBtnClick onClearBtnClick) {
        this.onClearBtnClick = onClearBtnClick;
    }

    public interface OnItemSelected {
        void OnItemSelected(int position, boolean isSelected);
    }

    private OnItemSelected onItemSelected;

    public void setOnItemSelected(OnItemSelected onItemSelected) {
        this.onItemSelected = onItemSelected;
    }

    public List<ShopCartModel> getmDatas() {
        return mDatas;
    }

    public void setOnAddClickedListener(OnAddClickedListener onAddClickedListener) {
        this.onAddClickedListener = onAddClickedListener;
    }

    public void setOnMinusClickedListener(OnMinusClickedListener onMinusClickedListener) {
        this.onMinusClickedListener = onMinusClickedListener;
    }

    public void setmDatas(List<ShopCartModel> mDatas) {
        this.mDatas = mDatas;
    }

}
